package com.example.excel_util.Import;

import org.jxls.reader.ReaderBuilder;
import org.jxls.reader.XLSReadStatus;
import org.jxls.reader.XLSReader;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 导入测试
 *
 * @author BAO KANG
 * @date 2020/7/23 10:07
 */
@Controller
@RequestMapping("employee")
public class EmployeeController {
    @RequestMapping("read")
    public void read(HttpServletRequest request, HttpServletResponse response) throws Exception {
/*        //配置文件
        InputStream inputXML = new BufferedInputStream(getClass().getResourceAsStream("classpath:/excel/xmlConfig.xml"));
        XLSReader mainReader = ReaderBuilder.buildFromXML(inputXML);
        //要导入的Excel
        InputStream inputXLS = new BufferedInputStream(getClass().getResourceAsStream("classpath:/excel/departmentdata.xls"));*/
//配置文件
        FileInputStream inputXML = new FileInputStream(new File("./src/main/resources/excel/xmlConfig.xml"));
        XLSReader mainReader = ReaderBuilder.buildFromXML(inputXML);
        //要导入的Excel
        FileInputStream inputXLS = new FileInputStream(new File("./src/main/resources/excel/departmentdata.xls"));
        Department department = new Department();
        Department hrDepartment = new Department();
        List<Staff> employees = new ArrayList();
        Map beans = new HashMap();
        beans.put("department", department);
        beans.put("hrDepartment", hrDepartment);
        beans.put("employees", employees);
        //todo：空指针
        XLSReadStatus readStatus = mainReader.read( inputXLS, beans);
        System.out.println(readStatus);
        //测试
        System.out.println(department.toString());
        for (Staff employee : employees) {
            System.out.println(employee.toString());
        }
    }
}
